import 'package:flutter/material.dart';
import 'package:figo_player_plugin/figo_player_plugin.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Figo Player Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomeScreen(),
    );
  }
}

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Figo Player Demo'),
      ),
      body: ListView(
        children: [
          ListTile(
            title: const Text('基本播放器'),
            subtitle: const Text('简单的视频播放器'),
            onTap: () => Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const BasicPlayerScreen()),
            ),
          ),
          ListTile(
            title: const Text('高级播放器'),
            subtitle: const Text('带控制面板的播放器'),
            onTap: () => Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const AdvancedPlayerScreen()),
            ),
          ),
          ListTile(
            title: const Text('播放列表'),
            subtitle: const Text('多文件播放列表'),
            onTap: () => Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => const PlaylistScreen()),
            ),
          ),
        ],
      ),
    );
  }
}

class BasicPlayerScreen extends StatefulWidget {
  const BasicPlayerScreen({super.key});

  @override
  _BasicPlayerScreenState createState() => _BasicPlayerScreenState();
}

class _BasicPlayerScreenState extends State<BasicPlayerScreen> {
  late FigoPlayerController _controller;
  bool _isPlaying = false;
  double _currentTime = 0.0;
  double _duration = 0.0;
  
  @override
  void initState() {
    super.initState();
    _controller = FigoPlayerController();
    _setupControllerListeners();
  }
  
  void _setupControllerListeners() {
    _controller.stateStream.listen((state) {
      setState(() {
        _isPlaying = state == PlayerState.playing;
      });
    });
    
    _controller.timeStream.listen((time) {
      setState(() {
        _currentTime = time;
      });
    });
    
    _controller.mediaInfoStream.listen((info) {
      setState(() {
        _duration = info.duration.inSeconds.toDouble();
      });
    });
    
    _controller.errorStream.listen((error) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('播放错误: $error')),
      );
    });
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('基本播放器'),
      ),
      body: Column(
        children: [
          // 播放器区域
          Expanded(
            child: Container(
              color: Colors.black,
              child: const Center(
                child: Text(
                  '视频播放区域\n(使用FFmpegKit)',
                  style: TextStyle(color: Colors.white, fontSize: 16),
                  textAlign: TextAlign.center,
                ),
              ),
            ),
          ),
          // 控制面板
          Container(
            padding: const EdgeInsets.all(16),
            child: Column(
              children: [
                // 进度条
                Slider(
                  value: _currentTime,
                  max: _duration,
                  onChanged: (value) {
                    _controller.seek(value);
                  },
                ),
                // 控制按钮
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    IconButton(
                      icon: Icon(_isPlaying ? Icons.pause : Icons.play_arrow),
                      onPressed: _togglePlayPause,
                    ),
                    IconButton(
                      icon: const Icon(Icons.stop),
                      onPressed: _stop,
                    ),
                    IconButton(
                      icon: const Icon(Icons.volume_up),
                      onPressed: _showVolumeDialog,
                    ),
                  ],
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
  
  void _togglePlayPause() {
    if (_isPlaying) {
      _controller.pause();
    } else {
      _controller.play();
    }
  }
  
  void _stop() {
    _controller.stop();
  }
  
  void _showVolumeDialog() {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text('音量控制'),
        content: StatefulBuilder(
          builder: (context, setState) {
            return Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text('当前音量: ${(_controller.volume * 100).round()}%'),
                Slider(
                  value: _controller.volume,
                  onChanged: (value) {
                    setState(() {
                      _controller.setVolume(value);
                    });
                  },
                ),
              ],
            );
          },
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.of(context).pop(),
            child: const Text('确定'),
          ),
        ],
      ),
    );
  }
  
  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}

class AdvancedPlayerScreen extends StatelessWidget {
  const AdvancedPlayerScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('高级播放器'),
      ),
      body: const Center(
        child: Text('高级播放器功能待实现'),
      ),
    );
  }
}

class PlaylistScreen extends StatelessWidget {
  const PlaylistScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('播放列表'),
      ),
      body: const Center(
        child: Text('播放列表功能待实现'),
      ),
    );
  }
}
